package com.developer.platform.domain.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.developer.platform.domain.User;
import com.developer.platform.domain.event.UserEvent;
import com.developer.platform.domain.event.UserEventPublisher;
import com.developer.platform.domain.event.UserRoleEvent;
import com.developer.platform.domain.repository.IUserRepository;
import com.developer.platform.domain.service.IUserRoleService;
import com.developer.platform.domain.service.IUserService;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.developer.platform.infrastructure.error.UserErrorType;
import com.veeker.core.exceptions.BusinessException;
import java.util.List;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Service
public class UserServiceImpl implements IUserService {

    private final IUserRepository userRepository;
    private final IUserRoleService userRoleService;
    private final PasswordEncoder passwordEncoder;
    private final UserEventPublisher userEventPublisher;

    public UserServiceImpl(IUserRepository userRepository,
                           IUserRoleService userRoleService,
                           PasswordEncoder passwordEncoder,
                           UserEventPublisher userEventPublisher) {
        this.userRepository = userRepository;
        this.userRoleService = userRoleService;
        this.passwordEncoder = passwordEncoder;
        this.userEventPublisher = userEventPublisher;
    }

    @Override
    public void save(User user, List<Long> roleIds) {
        BusinessException.isNonNull(userRepository.findByUsername(user.getUsername()),
            UserErrorType.USERNAME_ERROR);
        user.setAvatar("https://image.dapengjiaoyu.cn/2020/10/kgj0rdr5.gif");
        userRepository.save(user);
        userRoleService.save(user.getId(), roleIds);
    }

    @Override
    public void update(User user, List<Long> roleIds) {
        this.update(user);
        if(CollUtil.isNotEmpty(roleIds)){
            userRoleService.save(user.getId(), roleIds);
            if(!StatusEnum.N.equals(user.getStatus())){
                userEventPublisher.asyncPublish(new UserRoleEvent(user.getId()));
            }else {
                userEventPublisher.asyncPublish(new UserEvent(user.getId()));
            }
        }
    }

    @Override
    public void update(User user) {
        userRepository.updateById(user);
    }

    @Override
    public void updatePassword(long id, String newPassword, String oldPassword) {
        User user = findByUserId(id);
        BusinessException.isFalse(passwordEncoder.matches(oldPassword,user.getPassword()), UserErrorType.OLD_PASSWORD_ERROR);
        user.setPassword(passwordEncoder.encode(newPassword));
        this.update(user);
    }

    @Override
    public void updateStatus(long id) {
        User user = findByUserId(id);
        user.updateStatus();
        this.update(user);
        if(StatusEnum.N.equals(user.getStatus())){
            userEventPublisher.asyncPublish(new UserEvent(user.getId()));
        }
    }


    private User findByUserId(long id){
        User user = userRepository.findById(id);
        BusinessException.isNull(user, UserErrorType.USER_IS_NULL_ERROR);
        return user;
    }

}
